from random import random
from urllib import request
from urllib import parse
import random
import json
import hashlib

def translate_Word(en_str):
    URL = 'http://api.fanyi.baidu.com/api/trans/vip/translate'

    Form_Data = {}
    Form_Data['from'] = 'en'
    Form_Data['to'] = 'zh'
    Form_Data['q'] = en_str
    Form_Data['appid'] = '20241118002205797'
    Form_Data['salt'] = str(random.randint(32768, 65536))
    Key = "WoFrbZOfEYJ8FAW3Ws0D"
    m = Form_Data['appid'] + en_str + Form_Data['salt'] + Key
    m_md5 = hashlib.md5(m.encode('utf8'))
    Form_Data['sign'] = m_md5.hexdigest()

    data = parse.urlencode(Form_Data).encode('utf-8')
    response = request.urlopen(URL, data)
    html = response.read().decode('utf8')

    translate_result = json.loads(html)
    print(translate_result)
    translate_result = translate_result['trans_result'][0]['dst']

    return translate_result

if __name__ == '__main__':
    en_str = input('请输入要翻译的内容：')
    response = translate_Word(en_str)
    print("翻译结果是:%s" % (response))